package com.leo.component;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.component.Component;
import com.almasb.fxgl.physics.BoundingShape;
import com.almasb.fxgl.physics.HitBox;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import com.almasb.fxgl.time.LocalTimer;
import com.leo.constant.ArcheryType;
import javafx.util.Duration;

/**
 * @Author: wangss
 * @CreateTime: 2023-06-07  12:13
 * @Description: 弓箭
 */
public class BowComponent extends Component {

    private AnimatedTexture at;

    private LocalTimer shootTime;

    private Duration shootDelay = Duration.seconds(0.8);


    public BowComponent() {
        at = new AnimatedTexture(new AnimationChannel(FXGL.image("bow.png"), Duration.seconds(0.5), 4));
    }

    @Override
    public void onAdded() {
        shootTime = FXGL.newLocalTimer();
        entity.getViewComponent().addChild(at);
        entity.getBoundingBoxComponent().addHitBox(new HitBox(BoundingShape.box(686 / 4, 247)));
    }

    public void shoot() {
        if (shootTime.elapsed(shootDelay)) {

            Entity singleton = FXGL.getGameWorld().getSingleton(ArcheryType.SHOOT_BAR);
            ShootBarComponent component = singleton.getComponent(ShootBarComponent.class);

            FXGL.play("shoot.wav");
            at.play();
            FXGL.spawn("arrow", entity.getCenter().subtract(99 / 2.0, 11 / 2));
            shootTime.capture();
            component.reset();
        }
    }
}
